﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OA.SSO.Lib;

namespace OA.SSO.Demo
{
    public static class AuthorityValidate
    {
        private static string WebSiteCode;
        private static string AuthorityServiceUrl;
        private static string AuthorityUserName ;
        private static string AuthorityPassWord ;
        private static Service.SDK.Client client ;

        static AuthorityValidate()
        {
            WebSiteCode = System.Configuration.ConfigurationManager.AppSettings["WebSiteCode"];
            AuthorityServiceUrl = System.Configuration.ConfigurationManager.AppSettings["AuthorityServiceUrl"];
            AuthorityUserName = System.Configuration.ConfigurationManager.AppSettings["AuthorityUserName"];
            AuthorityPassWord = System.Configuration.ConfigurationManager.AppSettings["AuthorityPassWord"];
            client = new Service.SDK.Client(AuthorityServiceUrl, AuthorityUserName, AuthorityPassWord);
        }

        /// <summary>
        /// 验证站点权限
        /// </summary>
        public static Common.Result ValidateWebSiteAuthority()
        {
            return ValidateAuthority(WebSiteCode, null, null);
        }

        /// <summary>
        /// 验证菜单权限
        /// </summary>
        public static Common.Result ValidateMenuAuthority(string menuCode)
        {
            return ValidateAuthority(WebSiteCode, menuCode, null);
        }

        /// <summary>
        /// 验证按钮权限
        /// </summary>
        public static Common.Result ValidateButtonAuthority(string menuCode,string buttonCode)
        {
            return ValidateAuthority(WebSiteCode, menuCode, buttonCode);
        }

        /// <summary>
        /// 验证用户权限
        /// </summary>
        private static Common.Result ValidateAuthority(string website, string menuCode, string buttonCode)
        {
            Common.Result ret = new Common.Result();
            if (LoginPage.LoginUser.UserName == null)
            {
                ret.success = false;
                ret.message = "尚未登陆";
                return ret;
            }
            Service.Model.BaseResponse res = null;
            if (!string.IsNullOrEmpty(buttonCode))
            {
                res = client.Execute(new Service.Model.ValidateButtonAuthorityRequest()
                {
                    WebSiteCode = WebSiteCode,
                    MenuCode = menuCode,
                    ButtonCode = buttonCode,
                    UserName = LoginPage.LoginUser.UserName
                });
            }
            else if (!string.IsNullOrEmpty(menuCode))
            {
                res = client.Execute(new Service.Model.ValidateMenuAuthorityRequest()
                {
                    WebSiteCode = WebSiteCode,
                    MenuCode = menuCode,
                    UserName = LoginPage.LoginUser.UserName
                });
            }
            else
            {
                res = client.Execute(new Service.Model.ValidateWebSiteAuthorityRequest()
                {
                    WebSiteCode = WebSiteCode,
                    UserName = LoginPage.LoginUser.UserName
                });
            }
            ret.message = res.Message;
            ret.count = res.Code;
            ret.success = ret.count == 0;
            return ret;
        }
    }
}
